<template>
    <div class="app-container">
        <div style="text-align: end;">
            <el-button size="small" type="primary" @click="submitForm()">提交</el-button>
            <el-button size="small" @click="close()">返回</el-button>
        </div>
        <h4 class="form-header h4">基本信息</h4>
        <el-form ref="form" :model="form" label-width="80px">
            <el-row>
                <el-col :offset="2" :span="8">
                    <el-form-item label="用户昵称" prop="nickName">
                        <el-input v-model="form.nickName" disabled/>
                    </el-form-item>
                </el-col>
                <el-col :offset="2" :span="8">
                    <el-form-item label="登录账号" prop="userName">
                        <el-input v-model="form.userName" disabled/>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <h4 class="form-header h4">科室信息</h4>
        <el-form ref="formDept" :model="formDept" label-width="80px">
            <el-row>
                <el-col :offset="2" :span="8">
                    <el-form-item label="权限科室" prop="nickName">
                        <el-input v-model="form.deptGroup" disabled/>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <h4 class="form-header h4">角色信息</h4>
        <el-table ref="table" v-adaptive v-loading="loading" :data="roles.slice((pageNum-1)*pageSize,pageNum*pageSize)"
                  :row-key="getRowKey" :stripe='true'
                  border height="200px" @row-click="clickRow"
                  @selection-change="handleSelectionChange">
            <el-table-column align="center" label="序号" type="index">
                <template slot-scope="scope">
                    <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column :reserve-selection="true" type="selection" width="55"></el-table-column>
            <el-table-column align="center" label="角色编号" prop="roleId"/>
            <el-table-column align="center" label="角色名称" prop="roleName"/>
            <el-table-column align="center" label="权限字符" prop="roleKey"/>
            <el-table-column align="center" label="创建时间" prop="createTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :limit.sync="pageSize" :page.sync="pageNum" :total="total"/>

        <!-- <el-form label-width="100px">
          <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;">
            <el-button type="primary" size="small" @click="submitForm()">提交</el-button>
            <el-button size="small" @click="close()">返回</el-button>
          </el-form-item>
        </el-form> -->
    </div>
</template>

<script>
import {getAuthRole, updateAuthRole} from "@/api/system/user";

export default {
    name: "AuthRole",
    data () {
        return {
            // 遮罩层
            loading: true,
            // 分页信息
            total: 0,
            pageNum: 1,
            pageSize: 10,
            // 选中角色编号
            roleIds: [],
            // 角色信息
            roles: [],
            // 用户信息
            form: {},
            // 科室信息
            formDept: {}
        };
    },
    created () {
        const userId = this.$route.params && this.$route.params.userId;
        if (userId) {
            this.loading = true;
            getAuthRole(userId).then((response) => {
                this.form = response.data.user;
                this.roles = response.data.roles;
                this.total = this.roles.length;
                this.$nextTick(() => {
                    this.roles.forEach((row) => {
                        if (row.flag) {
                            this.$refs.table.toggleRowSelection(row);
                        }
                    });
                });
                this.loading = false;
            });
        }
    },
    methods: {
        /** 单击选中行数据 */
        clickRow (row) {
            this.$refs.table.toggleRowSelection(row);
        },
        // 多选框选中数据
        handleSelectionChange (selection) {
            this.roleIds = selection.map((item) => item.roleId);
        },
        // 保存选中的数据编号
        getRowKey (row) {
            return row.roleId;
        },
        /** 提交按钮 */
        submitForm () {
            const userId = this.form.userId;
            const roleIds = this.roleIds.join(",");
            updateAuthRole({userId: userId, roleIds: roleIds}).then((response) => {
                this.$modal.msgSuccess("授权成功");
                this.close();
            });
        },
        /** 关闭按钮 */
        close () {
            const obj = {path: "/system/user"};
            this.$tab.closeOpenPage(obj);
        },
    },
};
</script>
